<?php

//禁止浏览器缓冲,ajax输出必须使用
function NoCacheHeaders()
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
}

//URL跳转
function goURL($url='', $msg='')
{
    $url = ($url) ? $url : $_SERVER['HTTP_REFERER'];
    $JSstr = '';
    if ($msg)
    {
        $JSstr .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
        $JSstr .= "<script>alert('$msg');</script>\n";
    }
    $JSstr .= "<script>location.href='$url'</script>\n";
    $JSstr .= "<meta http-equiv=\"refresh\" content=\"1; url=$url\">";
    echo $JSstr;
    exit();
}

//html下拉菜单列表
function html_options($arr, $set='AaBa005353dffs')
{
    foreach ($arr as $k => $v)
    {
        if (is_array($v))
        {
            $options[] = "<optgroup title='$k'>";
            foreach ($v as $k2 => $v2)
            {
                $selected = ($set == $k) ? 'selected' : '';
                if (is_array($set))$selected = (in_array($k, $set)) ? 'selected' : '';
                $options[] = "<option value='$k2' $selected>$v2</option>";
            }
            $options[] = "</optgroup>";
        }
        else
        {
            $selected = ($set == $k) ? 'selected' : '';
            if (is_array($set))$selected = (in_array($k, $set)) ? 'selected' : '';
            $options[] = "<option value='$k' $selected>$v</option>";
        }
    }

    return implode("\n", $options);
}

//html下拉菜单生成
function html_select($idname='select', $options='', $set='AaBa00fte6t5', $js='', $size=0)
{
    $size = ($size == 0) ? '' : "size='$size'";
    $select = "<select name='$idname' id='$idname' $size $js style='font-size:12px;'>\n";
    if (is_array($options))$select .= html_options($options, $set);
    $select .= "</select>\n";

    return $select;
}

//radio列表
function html_radios($name, $array, $selected="", $sepreator="&nbsp;&nbsp;")
{
    $html = "";
    foreach ($array as $k => $v)
    {
        $checked = "";
        if ($k == $selected)$checked = "checked";
        $html .= "<label><input name=\"$name\" type=\"radio\" value=\"$k\" $checked> $v</label> $sepreator \n";
    }
    return $html;
}

//check列表
function html_checkboxes($name, $array, $selected=array(), $sepreator="&nbsp;&nbsp;")
{
    $html = "";
    //$sel = explode(',', $selected);
    foreach ($array as $k => $v)
    {
        $checked = "";
        for ($i = 0; $i < count($selected); $i++)
        {
            if ($selected[$i] == $k)$checked = "checked";
        }
        $html .= "<label><input name=\"$name\" type=\"checkbox\" value=\"$k\" $checked> $v</label> $sepreator \n";
    }
    return $html;
}



class smallCache
{
    public $id;
    public $path;

    function smallCache($path='/tmp')
    {
        $this->path = $path;
    }

    function get($id)
    {
        if(!$id)return false;
        $cache_file = "$this->path/".md5($id);
        
        if(is_file($cache_file))
        {
            $data = unserialize(file_get_contents($cache_file));
            return $data;
        }
        else
        {
            //echo $cache_file;
            return false;
        }
        
    }

    function set($id,$data="")
    {
        if(!$id)return false;
        $cache_file = "$this->path/".md5($id);
        file_put_contents($cache_file, serialize($data));
        return true;
    }

    function del($id)
    {
        if(!$id)return false;
        $cache_file = "$this->path/".md5($id);
        if(is_file($cache_file))
        {
            unlink($cache_file);
            return true;
        }
    }
}
?>